package view.servlet.category;

import com.alibaba.fastjson.JSON;
import pojo.Category;
import pojo.User;
import pojo.vo.Message;
import service.CategoryService;
import service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author wq3stone
 */
@WebServlet("/queryCategory")
public class QueryCategory extends HttpServlet {
    CategoryService categoryService = new CategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Message message = null;
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        User user = (User)req.getSession().getAttribute("user");
        String pcode = req.getParameter("pcode");
        if(pcode==null){
            message = categoryService.queryFirstCategory(user.getId());
        }else {
            message = categoryService.querySecondCategory(Integer.parseInt(pcode));
        }
        resp.getWriter().print(JSON.toJSONString(message));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
